<?php

use Swoole\Coroutine\Client;

// UDP 客户端
class UDPClient
{
    public function __construct($port = 9511)
    {
        go(function () use ($port) {
            $client = new Client(SWOOLE_SOCK_UDP);
            if (!$client->connect('127.0.0.1', $port, 0.5)) {
                echo "连接错误: " . $client->errCode . PHP_EOL;
            }
            fwrite(STDOUT, '请输入: ');
            $input = fgets(STDIN);
            $client->send($input);
            echo $client->recv();
            $client->close();
        });
    }
}